package com.me.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import com.me.data.MeEvent;
import com.me.fragment.MonthlyPageFragment;
import com.me.util.AppConstant;
import com.me.util.AppUtil;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class MonthlyPagerAdapter extends FragmentStatePagerAdapter {
	
	public MonthlyPagerAdapter(FragmentManager fragmentManager) {
		super(fragmentManager);

	}

	@Override
	public Fragment getItem(int position) {
		ArrayList<MeEvent> eventList = null;
		if (AppUtil.Event.eventType == AppConstant.EVENT_INDEX_PENDING) {
			
			eventList = AppUtil.App.dataService.retrievePendingEvent();
			
		} else {
			
			eventList = AppUtil.App.dataService.retrieveCalendarEvent(position);
			
		}
		MonthlyPageFragment fragment = MonthlyPageFragment.newInstance(eventList, position);
		return fragment;
	}

	@Override
	public int getCount() {
		if (AppUtil.Event.eventType == AppConstant.EVENT_INDEX_PENDING) {
			return 1;
		}
		return AppConstant.VIEW_PAGER_COUNT;
	}

	@Override
	public CharSequence getPageTitle(int position) {
		if (AppUtil.Event.eventType == AppConstant.EVENT_INDEX_PENDING) {
			return "All";
		}
		Calendar now = Calendar.getInstance();
		now.add(Calendar.MONTH, position - 12);
		SimpleDateFormat formatter = new SimpleDateFormat(
				AppConstant.VIEW_PAGE_TITLE);
		return formatter.format(now.getTime());
	}

}
